home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Language / Compiler / Builtins / InStream.m < prev    next >
Text File  |  1990-08-16  |  3KB  |  122 lines

  1. % @(#)real_InStream.m    1.3  3/16/88
  2. %
  3. import _VectorOfCharObject from "Builtins"
  4. export _InStreamObject to "Builtins"
  5.  
  6. const _InStreamObject == immutable object _InStreamObject
  7.   export getSignature, create
  8.   const InStreamType == type InStreamType
  9.     operation getChar -> [Character]
  10.     operation unGetChar [ Character ]
  11.     operation getString -> [ String ]
  12.     function eos -> [Boolean]
  13.     operation close
  14.   end InStreamType
  15.  
  16.   function getSignature -> [ r : Signature ]
  17.     r <- InStreamType
  18.   end getSignature
  19.  
  20.   operation create [ fd : Integer ] -> [r : InStreamType]
  21.     r <- object aUnixInStream
  22.       export getChar, unGetChar, getString, eos, close
  23.       const myfd : Integer == fd
  24.       monitor
  25.     var isBroken :    Boolean <- false
  26.     const BUFSIZE == 1024
  27.     const buffer == _VectorOfCharObject.create[BUFSIZE]
  28.     var maxValidIndex : Integer <- ~1
  29.     var minValidIndex : Integer <- 0
  30.     var isClosed :    Boolean <- myfd < 0
  31.  
  32.     operation unGetChar [r : Character]
  33.       if minValidIndex > 0 then
  34.         minValidIndex <- minValidIndex - 1
  35.         buffer(minValidIndex) := r
  36.       elseif maxValidIndex < minValidIndex then
  37.         % the buffer is empty
  38.         minValidIndex <- 0
  39.         maxValidIndex <- 0
  40.         buffer(minValidIndex) := r
  41.       else
  42.         returnAndFail
  43.       end if
  44.     end unGetChar
  45.  
  46.     operation getChar -> [r : Character]
  47.       if isClosed then returnAndFail end if
  48.       if maxValidIndex < minValidIndex then
  49.         % there is nothing in the buffer
  50.         if isBroken then
  51.           maxValidIndex <- ~1
  52.         else
  53.           primitive 016 [maxValidIndex] <- [myfd, buffer]
  54.         end if
  55.         minValidIndex <- 0
  56.       end if
  57.       if maxValidIndex < minValidIndex then
  58.         isClosed <- true
  59.         returnAndFail
  60.       else
  61.         r <- buffer(minValidIndex)
  62.         minValidIndex <- minValidIndex + 1
  63.       end if
  64.     end getChar
  65.  
  66.     operation getString -> [r : String]
  67.       var c : Character
  68.       r <- ""
  69.       if isClosed then returnAndFail end if
  70.       loop
  71.         exit when isClosed
  72.         if maxValidIndex < minValidIndex then
  73.           % there is nothing in the buffer
  74.           if isBroken then
  75.         maxValidIndex <- ~1
  76.           else
  77.         primitive 016 [maxValidIndex] <- [myfd, buffer]
  78.           end if
  79.           minValidIndex <- 0
  80.         end if
  81.         if maxValidIndex < minValidIndex then
  82.           isClosed <- true
  83.           exit
  84.         else
  85.           c <- buffer(minValidIndex)
  86.           minValidIndex <- minValidIndex + 1
  87.         end if
  88.         r <- r || c.asString
  89.         exit when c = '\^J'
  90.       end loop
  91.     end getString
  92.  
  93.     function eos -> [r : Boolean]
  94.       if isClosed then
  95.         r <- true
  96.       else
  97.         if maxValidIndex < minValidIndex then
  98.           % there is nothing in the buffer
  99.           if isBroken then
  100.         maxValidIndex <- ~1
  101.           else
  102.         primitive 016 [maxValidIndex] <- [myfd, buffer]
  103.           end if
  104.           minValidIndex <- 0
  105.         end if
  106.         isClosed <- maxValidIndex < minValidIndex
  107.         r <- isClosed
  108.       end if
  109.     end eos
  110.  
  111.     operation close 
  112.       isClosed <- true
  113.       maxValidIndex <- ~1
  114.       primitive 116 [] <- [myfd]
  115.     end close
  116.  
  117.       end monitor
  118.     end aUnixInStream
  119.   end create
  120. end _InStreamObject
  121.